package com.fbinggun.fbinggunutil.API;

import com.fbinggun.fbinggunutil.FBinggunUtil;
import com.fbinggun.fbinggunutil.utilclass.Variable;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class Exp4jAPI {



    public double calculate(String str, Variable... variables) {
        try {
            Expression expression = new ExpressionBuilder(str).build();
            if(variables != null) {
                for (Variable variable : variables) {
                    expression.setVariable(variable.getKey(), Double.parseDouble(variable.getValue()));
                }
            }
            return expression.evaluate();
        }catch(Exception e) {
            FBinggunUtil.plugin.getLogger().warning(str+" 计算公式错误 请检查相关配置 "+ str);
            return 0.0;
        }
    }

    public boolean isRunner(String formula, Variable... variables) {
        if (formula.contains("==")) {
            String[] papi = formula.split("==");
            if (papi[0].equalsIgnoreCase(papi[1])) {
                return true;
            } else {
                return false;
            }
        } else if (formula.contains("!=")) {
            String[] papi = formula.split("!=");
            if (papi[0].equalsIgnoreCase(papi[1])) {
                return false;
            } else {
                return true;
            }
        } else {

            try {
                return evaluateComparisonExpression(formula);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    private static final Pattern EXPRESSION_PATTERN = Pattern.compile("(\\d+(\\.\\d+)?)\\s*([<>=!]=?)\\s*(\\d+(\\.\\d+)?)");
    public static void main(String[] args) {
        String expression = "10+1==1";
        Exp4jAPI exp4jAPI = new Exp4jAPI();

        try {
            boolean result = exp4jAPI.evaluateComparisonExpression(expression);
            System.out.println("The result of the expression \"" + expression + "\" is: " + result);
        } catch (Exception e) {
            System.err.println("Error evaluating expression: " + e.getMessage());
        }
    }

    // 评估包含比较运算符的表达式
    protected  boolean evaluateComparisonExpression(String expression,Variable... variables) throws Exception {
//        Matcher matcher = EXPRESSION_PATTERN.matcher(expression);
//        if (!matcher.matches()) {
//            throw new IllegalArgumentException(expression+"计算公式错误 请检查相关配置");
//        }
//

        List<String> parts = splitWithOperators(expression);
        String leftExpr = parts.get(0);
        String operator = parts.get(1);
        String rightExpr = parts.get(2);
        double leftVal = calculate(leftExpr, variables);
        double rightVal = calculate(rightExpr, variables);
        return evaluateComparison(leftVal,operator,rightVal);
//
//        String leftExpr = matcher.group(1).trim().replaceAll("\\s+", ""); // 提取并清理左侧算术表达式
//        String rightExpr = matcher.group(2).trim().replaceAll("\\s+", ""); // 提取并清理右侧算术表达式
//        String operator = matcher.group(3).trim(); // 提取比较运算符
//
//        double leftValue = calculate(leftExpr,variables);
//        double rightValue = calculate(rightExpr,variables);

       // return evaluateComparison(leftValue, operator, rightValue);
    }


    public static List<String> splitWithOperators(String input) {
        List<String> result = new ArrayList<>();
        String regex = "([^<=>!]+)(<=|>=|<|>|==|!=)([^<=>!]*|$)";


        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            String left = matcher.group(1).trim();
            String operator = matcher.group(2);
            String right = matcher.group(3).trim();
            result.add(left);
            result.add(operator);
            result.add(right);
        }
            return result;
    }
    // 评估算术表达式
    protected  double evaluateArithmeticExpression(String expression) throws Exception {
        Expression exp = new ExpressionBuilder(expression).build();
        return exp.evaluate();
    }

    // 执行比较运算
    protected  boolean evaluateComparison(double left, String operator, double right) {
        switch (operator) {
            case "<":
                return left < right;
            case "<=":
                return left <= right;
            case ">":
                return left > right;
            case ">=":
                return left >= right;
                case "==":
                    return left == right;
                    case "!=":
                        return left != right;
            default:
                throw new IllegalArgumentException("计算公式错误 请检查相关配置: " + operator);
        }
    }
}